package com.czl.module_storehouse.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.czl.base.data.bean.tengyun.LocatListBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_service.event.SortBeanEvent;
import com.czl.module_storehouse.activity.baseInfo.GoodsBaseInfoActivity;
import com.czl.module_storehouse.adapter.StockListChildAdapter;
import com.czl.module_storehouse.databinding.LayoutRecyclerViewBinding;
import com.czl.module_storehouse.event.ScanSortEvent;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
public class StockListChildFragment extends Fragment {
    private LayoutRecyclerViewBinding mViewBinding;
    private List<SortBean> mSortBeanList;
    private List<LocatListBean> mLocatListBeanList;
    private String mName;

    public static StockListChildFragment getInstance(String tag, String name, ArrayList<SortBean> sortBeanList, ArrayList<LocatListBean> locatListBeanList) {
        StockListChildFragment fragment = new StockListChildFragment();
        Bundle bundle = new Bundle();
        bundle.putString("tag", tag);
        bundle.putString("name", name);
        bundle.putParcelableArrayList("sortBeanList", sortBeanList);
        bundle.putParcelableArrayList("locatListBeanList", locatListBeanList);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mViewBinding = LayoutRecyclerViewBinding.inflate(inflater, container, false);
        initData();
        return mViewBinding.getRoot();
    }

    protected void initData() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mName = arguments.getString("name");
            mSortBeanList = arguments.getParcelableArrayList("sortBeanList");
            mLocatListBeanList = arguments.getParcelableArrayList("locatListBeanList");
        }
        setupView();
    }

    private void setupView() {
        StockListChildAdapter adapter = new StockListChildAdapter(new ArrayList<>());
        mViewBinding.recyclerView.setAdapter(adapter);
        adapter.addSortAll(mSortBeanList, mName);
        adapter.addAll(mLocatListBeanList);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            Object item = adapter1.getItem(position);
            if (item instanceof SortBean) {
                SortBean sortBean = (SortBean) item;
                goActivity(sortBean);
            }
        });
    }

    private void goActivity(SortBean sortBean) {
        LiveEventBus.get(ScanSortEvent.class).postDelay(new ScanSortEvent(null, sortBean.getSortId()), 200);
        startActivity(new Intent(getContext(), GoodsBaseInfoActivity.class));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mViewBinding != null) {
            mViewBinding.unbind();
        }
    }
}
